__author__ = """Zach Seifts <zach.seifts@gmail.com>"""
__docformat__ = 'plaintext'


from Products.CMFCore.utils import getToolByName
from Products.CMFCore.WorkflowTool import addWorkflowFactory
from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
from Products.ExternalMethod.ExternalMethod import ExternalMethod
from Products.synchronicity.config import *

productname = 'synchronicity'

def setupTaskWorkFlow(self, workflow):
    """Define the TaskWorkFlow workflow.
    """

    workflow.setProperties(title='TaskWorkFlow')

    for s in ['Pending', 'Postponed', 'In Progress', 'Confirmed', 'Creating', 'Rejected', 'Resolved']:
        workflow.states.addState(s)

    for t in ['Confirmed', 'Resolve', 'Rejected', 'Postpone', 'In Progress', 'Submit']:
        workflow.transitions.addTransition(t)

    for v in ['review_history', 'comments', 'time', 'actor', 'action']:
        workflow.variables.addVariable(v)


    for l in []:
        if not l in workflow.worklists.objectValues():
            workflow.worklists.addWorklist(l)

    ## Initial State

    workflow.states.setInitialState('Creating')

    ## States initialization

    stateDef = workflow.states['Pending']
    stateDef.setProperties(title="""Pending""",
                           description="""""",
                           transitions=['Confirmed', 'Postpone', 'Rejected'])

    stateDef = workflow.states['Postponed']
    stateDef.setProperties(title="""Postponed""",
                           description="""""",
                           transitions=['Resolve'])

    stateDef = workflow.states['In Progress']
    stateDef.setProperties(title="""In Progress""",
                           description="""""",
                           transitions=['Resolve'])

    stateDef = workflow.states['Confirmed']
    stateDef.setProperties(title="""Confirmed""",
                           description="""""",
                           transitions=['In Progress'])

    stateDef = workflow.states['Creating']
    stateDef.setProperties(title="""Creating""",
                           description="""""",
                           transitions=['Submit'])

    stateDef = workflow.states['Rejected']
    stateDef.setProperties(title="""Rejected""",
                           description="""""",
                           transitions=[])

    stateDef = workflow.states['Resolved']
    stateDef.setProperties(title="""Resolved""",
                           description="""""",
                           transitions=[])

    ## Transitions initialization

    transitionDef = workflow.transitions['Confirmed']
    transitionDef.setProperties(title="""Confirmed""",
                                new_state_id="""Confirmed""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Confirmed""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Resolve']
    transitionDef.setProperties(title="""Resolve""",
                                new_state_id="""Confirmed""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Resolve""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Rejected']
    transitionDef.setProperties(title="""Rejected""",
                                new_state_id="""Rejected""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Rejected""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Postpone']
    transitionDef.setProperties(title="""Postpone""",
                                new_state_id="""Postponed""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Postpone""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['In Progress']
    transitionDef.setProperties(title="""In Progress""",
                                new_state_id="""In Progress""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""In Progress""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Submit']
    transitionDef.setProperties(title="""Submit""",
                                new_state_id="""Pending""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Submit""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    ## State Variable
    workflow.variables.setStateVar('review_state')

    ## Variables initialization
    variableDef = workflow.variables['review_history']
    variableDef.setProperties(description="""Provides access to workflow history""",
                              default_value="""""",
                              default_expr="""state_change/getHistory""",
                              for_catalog=0,
                              for_status=0,
                              update_always=0,
                              props={'guard_permissions': 'Request review; Review portal content'})

    variableDef = workflow.variables['comments']
    variableDef.setProperties(description="""Comments about the last transition""",
                              default_value="""""",
                              default_expr="""python:state_change.kwargs.get('comment', '')""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)

    variableDef = workflow.variables['time']
    variableDef.setProperties(description="""Time of the last transition""",
                              default_value="""""",
                              default_expr="""state_change/getDateTime""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)

    variableDef = workflow.variables['actor']
    variableDef.setProperties(description="""The ID of the user who performed the last transition""",
                              default_value="""""",
                              default_expr="""user/getId""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)

    variableDef = workflow.variables['action']
    variableDef.setProperties(description="""The last transition""",
                              default_value="""""",
                              default_expr="""transition/getId|nothing""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)


def createTaskWorkFlow(self, id):
    """Create the workflow for synchronicity.
    """

    ob = DCWorkflowDefinition(id)
    setupTaskWorkFlow(self, ob)
    return ob

addWorkflowFactory(createTaskWorkFlow,
                   id='TaskWorkFlow',
                   title='TaskWorkFlow')